package com.king.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@RestController
@RequestMapping("/acti")
public class ActiController {

    @GetMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.sendRedirect("https://wap.gd.10086.cn/nwap/products/productDetail/query.jsps?commCode=KJ10G&from=singlemessage");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/1")
    public String getMiguActi(String Sign, HttpServletResponse response) {
        try {
            System.out.println("Sign: " + Sign);
            PrintWriter writer = response.getWriter();
            writer.print("Sign: " + Sign);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    @GetMapping("/2")
    public String createMiguActi(HttpServletRequest request, HttpServletResponse response) {
        try {
            StringBuffer resultLink = new StringBuffer();
            String requestURL = request.getRequestURL().toString().replace("2","1");
            for (int i = 0; i< 5;i++){
                resultLink.append(requestURL+"?Sign="+ UUID.randomUUID().toString()+"\r\n\r\n");
            }
            System.out.println(resultLink.toString());
            PrintWriter writer = response.getWriter();
            writer.print("Links: \r\n\r\n" +resultLink.toString());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }
}
